dateUtils.js ➔ workingDays   B
last analyzed

Complexity

Conditions 8

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 28
rs 7.3333
c 0
b 0
f 0
cc 8
1
import dayjs from '../date';
2
3
const WEEKEND_INDEXES = new Set([ 0, 6 ]); // eslint-disable-line no-magic-numbers
4
5
export function workingDays({ include = [], exclude = [], to, from }) {
6
    const start = dayjs.min([ dayjs(from), ...include ]);
7
    const end = dayjs.max([ dayjs(to), ...include ]);
8
    const totalDays = dayjs(end).diff(dayjs(start), 'day') + 1;
9
    const days = [];
10
11
    for (let i = 0; i < totalDays; i++) {
12
        const day = dayjs(from).add(i, 'days').startOf('day');
13
14
        let insert = !WEEKEND_INDEXES.has(day.day());
15
16
        if (exclude.some(d => d.isSame(day, 'day'))) {
17
            insert = false;
18
        }
19
20
        if (day > dayjs(to) || day < dayjs(from)) {
21
            insert = false;
22
        }
23
24
        if (include.some(d => d.isSame(day, 'day'))) {
25
            insert = true;
26
        }
27
28
        if (insert) days.push(day);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
    }
30
31
    return days;
32
}
33